<template>
  <div class="order">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box_card">
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            class="input-with-select"
            placeholder="车牌号搜索"
            v-model="car_number_search"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="carnumber()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单信息列表区 -->
      <el-table :data="orderlist" border stripe class="tabel_box">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="车牌号" prop="car_number"></el-table-column>
        <el-table-column label="订单备注" prop="order_type"></el-table-column>
        <el-table-column
          label="订单创建时间"
          prop="money_sub_time"
        ></el-table-column>
        <el-table-column label="订单支付金额" prop="money">
          <template slot-scope="scope">
            <span>{{ scope.row.money }}元</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="delordermsg(scope.row.order_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <div class="page_box" v-if="ispage">
        <el-pagination
          :background="true"
          :page-count="pager_count"
          @current-change="handleCurrentChange"
          :layout="col"
          :total="count_total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      orderlist: [], //订单列表
      car_number_search: "", //车牌号搜索
      pager_count: 1, // 当前页码
      count_total: 0, // 总页数
      col: "prev, pager, next, jumper,slot", //开启的配置
      ispage: true, // 是否显示分页
    };
  },
  mounted() {
    this.getorderInfo(); //获取订单列表
  },
  watch: {
    // 如果只有1页，则不显示分页按钮
    count_total(newVal) {
      if (newVal <= 10) {
        this.ispage = false;
      }
    },
  },
  methods: {
    funorderinfo(parameter) {
      this.$getapi("/order/get", parameter, "json")
        .then((res) => {
          if (res.code === 200) {
            this.orderlist = res.data;
            this.count_total = res.count * 10;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 获取订单信息列表
    getorderInfo() {
      let par = {
        page: this.pager_count,
      };
      this.funorderinfo(par);
    },
    // 车牌号搜索
    carnumber() {
      let par = { car_number: this.car_number_search };
      this.funorderinfo(par);
    },
    // 页数跳转
    handleCurrentChange(val) {
      this.pager_count = val;
      this.getorderInfo();
    },
    // 删除订单提示
    delordermsg(order_id) {
      this.$confirm("是否删除该订单", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delorder(order_id);
        })
        .catch(() => {});
    },
    // 删除订单函数
    delorder(order_id) {
        this.$deleteapi("/order/delete", { order_id: order_id }, "json").then(res=>{
            if (res.code === 200){
                this.$message.success(res.msg);
                this.getorderInfo();//刷新
            }else{
                this.$message.error(res.msg);
            }
        }).catch((error) => {
            this.$message.error(error)
        })
    },
  },
};
</script>

<style lang="less" scoped>
.order {
  .box_card {
    margin-top: 20px;
    .tabel_box {
      font-size: 14px;
    }
    .page_box {
      margin-top: 20px;
    }
  }
  .el-row {
    margin-bottom: 10px;
  }
}
</style>